package org.vectrics.common.view;

/**
 * @author Mike Moore
 * @version 1.0
 */
import java.util.*;
import org.apache.log4j.Category;

public class MenuItem {
    private static Category log = Category.getInstance(MenuItem.class);
    public static int NORMAL_MODE = 0;
    public static int SELECT_MODE = 1;
    public static int HIDE_MODE = 2;
    public Menu menu = null;

    
    public MenuItem() {
    }
    
    private String id = "";
    //private MenuItem parent = null;
    private String description = null;
    private String link = null;
    private int mode = 0;
    private boolean visible = true;
    private Vector childList = new Vector();
    
    //private int top = 0;
    //private int left = 0;
    private String selectOnPath = null;

    public void setSelectOnPath(String path) {
        selectOnPath = path;
    }
    
    public String getSelectOnPath() {
        return(selectOnPath);
    }

    public boolean getHasChildren() {
        if (childList.size() > 0) {
            return(true);
        }
        else {
            return(false);
        }
    }

    public Menu getMenu() {
        return(menu);
    }
    
    public String getJavaScript() {
        log.debug("getJavaScript() - called.");

        StringBuffer str = new StringBuffer();
        if (getVisible()) {
            if ((description != null) && (description.trim().length() > 0)) {
                str.append("['" + description + "', ");
                if (link != null) {
                    str.append("'" + link + "'");
                }
                else {
                    str.append("null");
                }
                
                // Paramter that indicates selected.
                
                if (getMenu().getSelectedItem() == this) {
                    str.append(", 'true'");
                }
                else {
                    str.append(", 'false'");
                }
                
                
                str.append("\n");
            }
            
    
            if (getHasChildren()) {
                log.debug("getJavaScript() - has children.  Getting children script");
                str.append(", \n");
                str.append(getChildrenJavaScript());
            }
            else {
                log.debug("getJavaScript() - no children.");
            }
    
            if (description != null) {
                str.append("], ");
            }
        }
        return(str.toString());
    }

    public boolean getVisible() {
        return(visible);
    }

    public void setVisible(boolean value) {
        visible = value;
    }


    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getDescription() {
        return description;
    }
    public void setLink(String link) {
        this.link = link;
    }
    public String getLink() {
        return link;
    }
    public void setMode(int mode) {
        this.mode = mode;
    }
    public int getMode() {
        return mode;
    }

    public void addItem(MenuItem item) {
    //    item.parent = this;
        item.menu = getMenu();
        if (getMenu() == null) {
            log.error("addItem() - parent's menu must be set before adding children.");
        }
        childList.add(item);
    }

    
    public MenuItem addItem(String id, String desc, String link, int mode) {
        MenuItem newItem = new MenuItem();
        newItem.setId(id);
        newItem.setDescription(desc);
        newItem.setLink(link);
        newItem.setMode(mode);
        addItem(newItem);
        return(newItem);
    }

    protected Menu createMenu() {
        return(new Menu());
    }

    public Menu addChild() {
        Menu childMenu = createMenu();
        return(childMenu);
    }

    public void processAction(MenuItemAction action) {
        if (log.isDebugEnabled()) {
            log.debug("processAction() called");
            log.debug("processAction() - action.id = " + action.getId());
            log.debug("processAction() - action.parentId = " + action.getParent());
            log.debug("processAction() - action.visible = " + action.getVisible());
        }
        String itemId = action.getId();
        String parentId = action.getParent();

        MenuItem item = getChildInTree(itemId);
        MenuItem parentItem = null;

        if (parentId != null) {
            parentItem = getChildInTree(parentId);
        }


        if (item == null) {
            log.debug("processAction() - creating item: " + action.getId());
            item = new MenuItem();
            item.setDescription(action.getDescription());
            item.setId(action.getId());
            item.setLink(action.getLink());
            item.setSelectOnPath(action.getSelectOnPath());
        }
        item.setVisible(action.getVisible());


        if (parentItem != null) {
            log.debug("processAction() - adding item to parent");
            parentItem.addItem(item);
        }
        else {
            this.addItem(item);
        }

    }

    public boolean hasChild(MenuItem item) {
        boolean found = false;
        Iterator iter = childList.iterator();
        while (iter.hasNext()) {
            MenuItem curItem = (MenuItem)iter.next();
            if (curItem == item) {
                found = true;
            }
            else {
                found = curItem.hasChild(item);
            }
        }
        return(found);
    }

    public MenuItem getChildInTree(String childId) {
        MenuItem found = null;
        if (childId.equals(getId())) {
            found = this;
        }
        Iterator iter = childList.iterator();
        while ((iter.hasNext()) && (found == null)) {
            MenuItem curItem = (MenuItem)iter.next();
            if (curItem.getId().equals(childId)) {
                found = curItem;
            }
            else  {
                found = curItem.getChildInTree(childId);
            }

        }
        return(found);
    }

    public MenuItem getChild(String childId) {
        MenuItem found = null;
        if (childId.equals(getId())) {
            found = this;
        }
        Iterator iter = childList.iterator();
        while ((iter.hasNext()) && (found == null)) {
            MenuItem curItem = (MenuItem)iter.next();
            if (curItem.getId().equals(childId)) {
                found = curItem;
            }

        }
        return(found);
    }

    public MenuItem addItem(String id, String desc, String link) {
        MenuItem newItem = new MenuItem();
        newItem.setId(id);
        newItem.setDescription(desc);
        newItem.setLink(link);
        newItem.setMode(MenuItem.NORMAL_MODE);
        addItem(newItem);
        return(newItem);
    }

    public Vector getChildList() {
        return(childList);
    }


    public String getChildrenJavaScript() {
        StringBuffer str = new StringBuffer();
        Iterator iter = childList.iterator();
        while (iter.hasNext()) {
            MenuItem item = (MenuItem)iter.next();
            str.append(item.getJavaScript());
        }
        return(str.toString());
    }



    public MenuItem findItemWithId(String id) {
        MenuItem foundItem = null;
        Iterator iter = childList.iterator();
        while (iter.hasNext()) {
            MenuItem item = (MenuItem)iter.next();
            if (item.getId().compareTo(id) == 0) {
                foundItem = item;
            }
        }
        return(foundItem);
    }

}

 